package com.service.admin.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.order.controller.req.AddOrderHrComboPaddingReq;
import com.service.admin.order.controller.req.PageOrderHrComboPaddingReq;
import com.service.admin.order.service.OrderProductHrComboDetailsService;
import com.service.admin.order.service.OrderProductHrComboPaddingService;
import com.service.admin.order.service.OrderProductHrComboService;
import com.service.admin.product.service.ProductHrComboService;
import com.service.admin.support.OrderProductHrComboLockUtils;
import com.service.admin.utils.DateUtils;
import com.service.admin.utils.MathUtil;
import com.service.core.exception.CommonException;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.dto.rep.OrderProductHrComboPaddingRep;
import com.service.dao.enums.HashRateOrderEnums;
import com.service.dao.mapper.OrderProductHrComboPaddingMapper;
import com.service.dao.model.OrderProductHrCombo;
import com.service.dao.model.OrderProductHrComboDetails;
import com.service.dao.model.OrderProductHrComboPadding;
import com.service.dao.model.ProductHrCombo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service("orderProductHrComboPaddingService")
public class OrderProductHrComboPaddingServiceImpl extends ServiceImpl<OrderProductHrComboPaddingMapper, OrderProductHrComboPadding> implements OrderProductHrComboPaddingService {
	@Autowired
	private OrderProductHrComboService orderProductHrComboService;
	@Autowired
	private OrderProductHrComboDetailsService orderProductHrComboDetailsService;
	@Autowired
	private ProductHrComboService productHrComboService;
	@Autowired
	private OrderProductHrComboLockUtils orderProductHrComboLockUtils;

	/**
	 * @param paddingReq
	 * @Description: 新增填充算力记录
	 * @Author: ty
	 * @Date: 2021/6/8 19:18
	 * @param: [paddingReq]
	 * @return: void
	 */
	@Override
	public void addPaddingHashRate(AddOrderHrComboPaddingReq paddingReq) {
		//获取订单
		OrderProductHrCombo orderProductHrCombo = orderProductHrComboService.getById(paddingReq.getOrderId());

        if (ObjectUtils.isEmpty(orderProductHrCombo)) {
            throw new CommonException("订单信息未找到！");
        }
        BigDecimal lastPadding = MathUtil.add(orderProductHrCombo.getTotalHashRate(), orderProductHrCombo.getTotalPaddingHashRate(), 4);
        if (lastPadding.compareTo(paddingReq.getPaddingNum()) < 0) {
            throw new CommonException("最大可填充算力为:" + lastPadding);
        }
        //获取订单详情
        OrderProductHrComboDetails orderDetails = orderProductHrComboDetailsService.getOne(new QueryWrapper<OrderProductHrComboDetails>().lambda()
                .eq(OrderProductHrComboDetails::getHrOrderId, orderProductHrCombo.getId())
        );
        if (ObjectUtils.isEmpty(orderDetails)) {
            throw new CommonException("订单详情未找到！");
        }
        //反追溯产品信息
        ProductHrCombo combo = productHrComboService.getById(orderDetails.getComboId());
        if (ObjectUtils.isEmpty(combo)) {
            throw new CommonException("订单购买的产品信息未找到或已下架！");
        }
        boolean isLock = orderProductHrComboLockUtils.lock(orderProductHrCombo.getHrOrderNo());
        if (!isLock) {
            throw new CommonException("正在填充该订单，请稍后处理！");
        }
        try {
            //根据产品封装期计算填充开始时间
            OrderProductHrComboPadding padding = new OrderProductHrComboPadding();
            padding.setCoinsId(combo.getCoinsId());
            padding.setCoinsName(combo.getCoinsName());
            padding.setMemberId(orderProductHrCombo.getMemberId());
            padding.setHrOrderId(orderProductHrCombo.getId());
            padding.setCustodyFee(combo.getCustodyFee());
            padding.setPaddingHashRate(paddingReq.getPaddingNum());
            padding.setTotalIncome(BigDecimal.ZERO);
            Date startTime = DateUtils.addDay(new Date(), combo.getPackageDays());
            Date endTime = DateUtils.addDay(startTime, combo.getDeadline());
            padding.setPaddingStartTime(startTime);
            padding.setPaddingStatus(HashRateOrderEnums.Status.payed.getCode());
            padding.setPaddingEndTime(endTime);
            save(padding);
            //修改订单记录已填充数值
            BigDecimal paddingHashRate = MathUtil.add(orderProductHrCombo.getTotalPaddingHashRate(), paddingReq.getPaddingNum(), 4);
            orderProductHrCombo.setTotalPaddingHashRate(paddingHashRate);
            orderProductHrCombo.setOrderStatus(HashRateOrderEnums.Status.payed_effective.getCode());
            orderProductHrComboService.updateById(orderProductHrCombo);
        } finally {
            orderProductHrComboLockUtils.unlock(orderProductHrCombo.getHrOrderNo());
        }
    }


	/**
     * 填充算力流水
     *
     * @return
     */
    @Override
    public CommonPageRep<OrderProductHrComboPaddingRep> memberPaddingList(PageOrderHrComboPaddingReq req) {
        //设置分页信息
        Page<OrderProductHrComboPaddingRep> page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<OrderProductHrComboPaddingRep> orderProductHrComboPaddingReps = baseMapper.memberPaddingList(req.getUserAccount(),
                req.getMobile(), req.getStartTime(), req.getEndTime());
        return CommonPageRep.buildResultRep(page, orderProductHrComboPaddingReps);
    }
}
